package com.inossem.utils.scan;

import android.app.Activity;
import android.content.Intent;
import android.text.TextUtils;

import static com.inossem.utils.scan.ScanActivity.KEY_SCAN_PARAMETER;

/**
 * Created by leij on 2017/10/31.
 */

public class ScanUtils {

    public static void openScanActivity(Activity activity) {
        openScanActivity(activity, null);
    }

    public static void openScanActivity(Activity activity, ScanParameter scanParameter) {
        Intent intent = new Intent(activity, ScanActivity.class);
        intent.putExtra(KEY_SCAN_PARAMETER, scanParameter);
        activity.startActivityForResult(intent, ScanActivity.REQUEST_CODE_SCAN);
    }

    public static String getScanResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == ScanActivity.REQUEST_CODE_SCAN && resultCode == Activity.RESULT_OK && data != null) {
            String result = data.getStringExtra(ScanActivity.KEY_SCAN_RESULT);
            return TextUtils.isEmpty(result) ? null : result;
        }
        return null;
    }

    public interface ScanCodeListener {
        void onResult(String scanResult);
    }

}
